package com.stormrage.edu.ctrl.notice;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.stormrage.edu.serv.adm.AdmAcctServ;
import com.stormrage.edu.serv.learn.LeaBunchServ;
import com.stormrage.edu.serv.student.StuNoticeServ;
import com.stormrage.entity.adm.AdmRes;
import com.stormrage.entity.learn.rep.LeaBunchRep;
import com.stormrage.entity.rep.ErrorRep;
import com.stormrage.entity.shiro.ShiroAcct;
import com.stormrage.entity.student.StuNotice;
import com.stormrage.entity.student.rep.StuNoticeRep;
import com.stormrage.shiro.bind.CurrentUser;
import com.stormragetech.core.ctrl.BaseCtrl;

/**
 * @Title: StuNoticeCtrl.java
 * @Package com.stormrage.edu.ctrl.notice
 * @Description: 通知 Controller
 * @author www.stormragetech.com
 * @date 2016年6月1日
 * @version V1.0
 */
@Controller
@RequestMapping("/notice")
public class StuNoticeCtrl extends BaseCtrl {

	private static final Logger logger = LoggerFactory.getLogger(StuNoticeCtrl.class);

	@Value("#{constants['html.title']}")
	private String htmlTitle;

	@Value("#{constants['path.root']}")
	private String rootPath;

	@Value("#{constants['path.image']}")
	private String imagePath;

	@Autowired
	private AdmAcctServ admAcctServ;

	@Autowired
	private LeaBunchServ leaBunchServ;

	@Autowired
	private StuNoticeServ stuNoticeServ;

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String index(@CurrentUser ShiroAcct curr, Model model) {

		logger.debug(" 【 CONTROLLER 】- 【 INDEX 】- 开始运行 ");

		try {

			// ------------------ 获取用户操作的 Menu ------------------
			List<AdmRes> menus = admAcctServ.queryMenuById(Integer.valueOf(curr.getId()));

			model.addAttribute("user", curr);
			model.addAttribute("menus", menus);
			model.addAttribute("htmlTitle", htmlTitle);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		return "notice/index";
	}

	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String create(@CurrentUser ShiroAcct curr, Model model) {

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 create 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户操作的 Menu ------------------
			List<AdmRes> menus = admAcctServ.queryMenuById(Integer.valueOf(curr.getId()));

			// ------------------ Model 赋值 ------------------
			model.addAttribute("user", curr);
			model.addAttribute("menus", menus);
			model.addAttribute("htmlTitle", htmlTitle);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 create 】-【 GET 】-【 COMPLETED 】");
		return "notice/create";
	}

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String create(StuNotice entity, RedirectAttributes redirect) {

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 create 】-【 POST 】-【 STARTED 】");

		ErrorRep err = null;
		String errMsg = "";

		try {

			// ------------------ Ctrl操作 ------------------

			Integer index = stuNoticeServ.create(entity);
			errMsg = "提示信息：添加编号为【 " + index + " 】的通知成功";
			err = ErrorRep.instance(true, errMsg);
			redirect.addFlashAttribute("err", err);

		} catch (Exception e) {
			errMsg = "提示信息：添加通知失败";
			err = ErrorRep.instance(false, errMsg);
			redirect.addFlashAttribute("err", err);
			return "redirect:/notice/create";
		}
		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 create 】-【 POST 】-【 COMPLETED 】");
		return "redirect:/notice/list";
	}

	@RequestMapping("/list")
	public String list(@CurrentUser ShiroAcct curr, Model model) {

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 list 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户操作的 Menu ------------------
			List<AdmRes> menus = admAcctServ.queryMenuById(Integer.valueOf(curr.getId()));

			// ------------------ 获取全体学员通知 ------------------
			List<StuNoticeRep> array = stuNoticeServ.queryRepListByBunchId(0);

			// ------------------ Model 赋值 ------------------
			model.addAttribute("user", curr);
			model.addAttribute("menus", menus);
			model.addAttribute("htmlTitle", htmlTitle);
			model.addAttribute("data", array);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 list 】-【 GET 】-【 COMPLETED 】");
		return "notice/list";
	}

	@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
	public String edit(@CurrentUser ShiroAcct curr, @PathVariable("id") Integer id, Model model) {

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 edit 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户操作的 Menu ------------------
			List<AdmRes> menus = admAcctServ.queryMenuById(Integer.valueOf(curr.getId()));

			// ------------------ 获取全体学员通知 ------------------
			StuNotice entity = stuNoticeServ.queryById(id);

			// ------------------ Model 赋值 ------------------
			model.addAttribute("user", curr);
			model.addAttribute("menus", menus);
			model.addAttribute("htmlTitle", htmlTitle);
			model.addAttribute("data", entity);
		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 edit 】-【 GET 】-【 COMPLETED 】");
		return "notice/edit";
	}

	@RequestMapping(value = "/{id}/edit", method = RequestMethod.POST)
	public String edit(StuNotice entity, RedirectAttributes redirect) {

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 edit 】-【 POST 】-【 STARTED 】");

		ErrorRep err = null;
		String errMsg = "";

		try {

			// ------------------ Ctrl操作 ------------------

			stuNoticeServ.update(entity);
			errMsg = "提示信息：修改编号为【 " + entity.getId() + " 】的通知成功";
			err = ErrorRep.instance(true, errMsg);
			redirect.addFlashAttribute("err", err);

		} catch (Exception e) {
			errMsg = "提示信息：修改编号为【 " + entity.getId() + " 】的通知失败";
			err = ErrorRep.instance(false, errMsg);
			redirect.addFlashAttribute("err", err);
			return "redirect:/notice/edit";
		}
		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 edit 】-【 POST 】-【 COMPLETED 】");
		return "redirect:/notice/list";
	}

	@RequestMapping("/{id}/detail")
	public String detail(@CurrentUser ShiroAcct curr, @PathVariable("id") Integer id, Model model) {

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 detail 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户操作的 Menu ------------------
			List<AdmRes> menus = admAcctServ.queryMenuById(Integer.valueOf(curr.getId()));

			// ------------------ 获取全体学员通知 ------------------
			StuNotice entity = stuNoticeServ.queryById(id);

			// ------------------ Model 赋值 ------------------
			model.addAttribute("user", curr);
			model.addAttribute("menus", menus);
			model.addAttribute("htmlTitle", htmlTitle);
			model.addAttribute("data", entity);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 list 】-【 GET 】-【 COMPLETED 】");
		return "notice/detail";
	}

	@RequestMapping("/bunch/")
	public String bunch(@CurrentUser ShiroAcct curr, Model model) {

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 bunch 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户操作的 Menu ------------------
			List<AdmRes> menus = admAcctServ.queryMenuById(Integer.valueOf(curr.getId()));

			// ------------------ 获取全体学员通知 ------------------
			List<LeaBunchRep> array = leaBunchServ.queryRepListForNotice();

			// ------------------ Model 赋值 ------------------
			model.addAttribute("user", curr);
			model.addAttribute("menus", menus);
			model.addAttribute("htmlTitle", htmlTitle);
			model.addAttribute("data", array);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 bunch 】-【 GET 】-【 COMPLETED 】");
		return "notice/bunch/bunch";
	}

	@RequestMapping(value = "/bunch/{id}/create", method = RequestMethod.GET)
	public String create(@CurrentUser ShiroAcct curr, @PathVariable("id") Integer id, Model model) {

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 create 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户操作的 Menu ------------------
			List<AdmRes> menus = admAcctServ.queryMenuById(Integer.valueOf(curr.getId()));

			// ------------------ Model 赋值 ------------------
			model.addAttribute("user", curr);
			model.addAttribute("menus", menus);
			model.addAttribute("htmlTitle", htmlTitle);
			model.addAttribute("id", id);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 create 】-【 GET 】-【 COMPLETED 】");
		return "notice/bunch/create";
	}

	@RequestMapping(value = "/bunch/{id}/create", method = RequestMethod.POST)
	public String create(StuNotice entity, @PathVariable("id") Integer id, RedirectAttributes redirect) {

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 create 】-【 POST 】-【 STARTED 】");

		ErrorRep err = null;
		String errMsg = "";

		try {

			// ------------------ Ctrl操作 ------------------

			Integer index = stuNoticeServ.create(entity);
			errMsg = "提示信息：添加编号为【 " + index + " 】的通知成功";
			err = ErrorRep.instance(true, errMsg);
			redirect.addFlashAttribute("err", err);

		} catch (Exception e) {
			errMsg = "提示信息：添加通知失败";
			err = ErrorRep.instance(false, errMsg);
			redirect.addFlashAttribute("err", err);
			return "redirect:/notice/bunch/{id}/create";
		}
		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 create 】-【 POST 】-【 COMPLETED 】");
		return "redirect:/notice/bunch/{id}/list";
	}

	@RequestMapping("/bunch/{id}/list")
	public String list(@CurrentUser ShiroAcct curr, @PathVariable("id") Integer id, Model model) {

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 list 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户操作的 Menu ------------------
			List<AdmRes> menus = admAcctServ.queryMenuById(Integer.valueOf(curr.getId()));

			// ------------------ 获取全体学员通知 ------------------
			List<StuNoticeRep> array = stuNoticeServ.queryRepListByBunchId(id);

			// ------------------ Model 赋值 ------------------
			model.addAttribute("user", curr);
			model.addAttribute("menus", menus);
			model.addAttribute("htmlTitle", htmlTitle);
			model.addAttribute("data", array);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 list 】-【 GET 】-【 COMPLETED 】");
		return "notice/bunch/list";
	}

	@RequestMapping(value = "/bunch/{aid}/{id}/edit", method = RequestMethod.GET)
	public String edit(@CurrentUser ShiroAcct curr, @PathVariable("aid") Integer aid, @PathVariable("id") Integer id,
			Model model) {

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 edit 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户操作的 Menu ------------------
			List<AdmRes> menus = admAcctServ.queryMenuById(Integer.valueOf(curr.getId()));

			// ------------------ 获取全体学员通知 ------------------
			StuNotice entity = stuNoticeServ.queryById(id);

			// ------------------ Model 赋值 ------------------
			model.addAttribute("user", curr);
			model.addAttribute("menus", menus);
			model.addAttribute("htmlTitle", htmlTitle);
			model.addAttribute("data", entity);
			model.addAttribute("id", aid);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 edit 】-【 GET 】-【 COMPLETED 】");
		return "notice/bunch/edit";
	}

	@RequestMapping(value = "/bunch/{aid}/{id}/edit", method = RequestMethod.POST)
	public String edit(StuNotice entity, @PathVariable("id") Integer id, RedirectAttributes redirect) {

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 edit 】-【 POST 】-【 STARTED 】");

		ErrorRep err = null;
		String errMsg = "";

		try {

			// ------------------ Ctrl操作 ------------------

			stuNoticeServ.update(entity);
			errMsg = "提示信息：修改编号为【 " + entity.getId() + " 】的通知成功";
			err = ErrorRep.instance(true, errMsg);
			redirect.addFlashAttribute("err", err);

		} catch (Exception e) {
			errMsg = "提示信息：修改编号为【 " + entity.getId() + " 】的通知失败";
			err = ErrorRep.instance(false, errMsg);
			redirect.addFlashAttribute("err", err);
			return "redirect:/notice/bunch/{aid}/{id}/edit";
		}
		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 edit 】-【 POST 】-【 COMPLETED 】");
		return "redirect:/notice/bunch/{aid}/list";
	}

	@RequestMapping("/bunch/{aid}/{id}/detail")
	public String detail(@CurrentUser ShiroAcct curr, @PathVariable("aid") Integer aid, @PathVariable("id") Integer id,
			Model model) {

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 detail 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户操作的 Menu ------------------
			List<AdmRes> menus = admAcctServ.queryMenuById(Integer.valueOf(curr.getId()));

			// ------------------ 获取全体学员通知 ------------------
			StuNotice entity = stuNoticeServ.queryById(id);

			// ------------------ Model 赋值 ------------------
			model.addAttribute("user", curr);
			model.addAttribute("menus", menus);
			model.addAttribute("htmlTitle", htmlTitle);
			model.addAttribute("data", entity);
			model.addAttribute("id", aid);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 StuNoticeCtrl 】-【 list 】-【 GET 】-【 COMPLETED 】");
		return "notice/bunch/detail";
	}

}
